package cn.edu.live.controller;

import cn.edu.live.dao.AnchorDao;
import cn.edu.live.model.Anchor;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;

public class AnchorController extends Controller {

	/**
	 * 主播个人主页，主播自己看的
	 */
	public void homePage() {

		int code = -1;
		String msg = "未查找到该主播老师！";

		// 默认使用infoid为全局唯一标识
		Object sinfoid = getSession().getAttribute("infoid");
		// int type = (int) getSession().getAttribute("type");

		// session中没有该id
		if (sinfoid == null) {
			msg = "未登录！";
			renderJson(respJson(code, msg));
			return;
		}

		int infoid = (int) sinfoid;
		Anchor anchor = new AnchorDao().findByInfoid(infoid);

		// 未找到该id
		if (anchor == null) {
			renderJson(respJson(code, msg));
			return;
		}

		// TODO 找到主播数据，添加到页面中
		renderJsp("");

	}

	public void getHomePage() {
		int code = -1;
		String msg = "未查找到该直播老师！";

		Integer infoid = getParaToInt("infoid");
		// 参数为空
		if (infoid == null) {
			msg = "直播老师id错误！";
			renderJson(respJson(code, msg));
			return;
		}

		Anchor anchor = new AnchorDao().findByInfoid(infoid);

		// 未找到该id
		if (anchor == null) {
			renderJson(respJson(code, msg));
			return;
		}

		// TODO 找到主播数据，添加到页面中
		renderJsp("");
	}

	/**
	 * 拼接返回json对象用
	 * 
	 * @param code
	 * @param errMsg
	 * @return
	 */
	private JSONObject respJson(int code, String msg) {
		JSONObject js = new JSONObject();
		js.put("code", code);
		js.put("msg", msg);
		return js;
	}

	private JSONObject respJson(int code, String msg, JSONObject data) {
		JSONObject js = new JSONObject();
		js.put("code", code);
		js.put("msg", msg);
		js.put("data", data);
		return js;
	}

}
